package com.yatang.yiliao.app.fragments;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.yatang.yiliao.app.R;
import com.yatang.yiliao.app.activities.TropicListActivity;
import com.yatang.yiliao.app.utils.MyCountTimer;


public class TropicFragment extends Fragment implements View.OnClickListener, DialogInterface.OnKeyListener {


    private Dialog loadingDialog;
    public static final int TIME_COUNT = 10000;//时间防止从59s开始显示
    private MyCountTimer time;

    public TropicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_tropic, container, false);
        time = new MyCountTimer(TIME_COUNT, 1000);
        Button btnSearch = (Button) ret.findViewById(R.id.btn_search_tropic);
        btnSearch.setOnClickListener(this);
        Button btnTropicList = (Button)ret.findViewById(R.id.btn_show_tropic);
        btnTropicList.setOnClickListener(this);
        return ret;
    }


    public static Dialog createLoadingDialog(Context context, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                context, R.anim.loading_animation);
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog

        loadingDialog.setCancelable(true);// 不可以用“返回键”取消
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        return loadingDialog;

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_search_tropic:
                loadingDialog = createLoadingDialog(getActivity(), "正在匹配，请稍后...");
                loadingDialog.show();
                loadingDialog.setOnKeyListener(this);
                time.setLoadingDialog(loadingDialog);
                time.start();
                break;
            case R.id.btn_show_tropic:
                Intent intent = new Intent(getActivity(), TropicListActivity.class);
                startActivity(intent);
        }
    }


    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {  //表示按返回键 时的操作
                if (loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                    time.cancel();
                    AlertDialog.Builder builder
                            = new AlertDialog.Builder(getActivity());
                    builder.setTitle("提示")
                            .setMessage("话题未能匹配，是否上传本话题到服务器端以备搜索？（注意：每天只可以上传一个话题）")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(getActivity(),"话题上传成功！",Toast.LENGTH_SHORT).show();
                                    // 对话框消失
                                    dialog.dismiss();
                                }
                            })  // 设置确定按钮，第二个参数就是确定按钮点击的对话框点击事件处理
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 对话框消失
                                    dialog.dismiss();
                                }
                            }) // 设置否定按钮（取消按钮）
                                    // 设置对话框的图标
                            .setIcon(R.drawable.ic_launcher)
                    ;
                    AlertDialog dialog1 = builder.create();
                    dialog1.show();
                    return true;
                }
            }
        }
        return false;
    }
}
